<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div id="mailCompose">
    <div class="callout callout-danger error_div" style="display:none;">
        <h4>Warning!</h4>

        <p class="error_msg"></p>
    </div>

    <div class="callout callout-info" id="draftSaved" style="display: none;position: absolute;z-index: 1000;left:200px">

        <p >当前编辑的邮件已保存到草稿箱</p>
    </div>
    <div class="box-header with-border">
        <h3 class="box-title">发送新消息</h3>
    </div>
    <!-- /.box-header -->
    <div class="box-body">
        <input type="hidden" value="${draftId}" id="driftId"/>
        <div class="form-group">
            <input class="form-control" placeholder="To:" id="to"
                   value="${draft.to}">
        </div>
        <div class="form-group">
            <input class="form-control" placeholder="CC:(可选)" id="cc" value="${draft.cc}">

        </div>
        <div class="form-group">
            <input class="form-control" placeholder="BCC:(可选)" id="bcc" value="${draft.bcc}">
        </div>
        <div class="form-group">
            <input class="form-control" placeholder="Subject:" id="subject" value="${draft.subject}">
        </div>
        <%--<div class="form-group">--%>
            <%--<textarea id="compose-textarea" class="form-control"--%>
                      <%--style="height: 300px">--%>
                <%--${draft.content}--%>
            <%--</textarea>--%>

            <script id="container" name="content" type="text/plain">
                ${draft.content}
            </script>
    <%--</div>--%>
        <div class="form-group">
            <%--<form action="${pageContext.request.contextPath}/mail/uploadAttachment.shtml" id="uploadForm" method="post" enctype="multipart/form-data">--%>
            <a class="btn btn-default" id="addAttachment" style="margin-top: 5px;">
                <i class="fa fa-paperclip"></i> 添加附件
            </a>
            <%--</form>--%>
        </div>
        <%--<form action="${pageContext.request.contextPath}/mail/upload.shtml" method="post" enctype="multipart/form-data">--%>
            <%--<div class="form-group">--%>
                <%--<input type="file" name="file" class="btn btn-default" >--%>
                <%--<input type="submit" value="添加附件" class="btn btn-default">--%>
            <%--</div>--%>
            <%--<p class="help-block">最大. 32MB</p>--%>

        <%--</form>--%>

        <div class="form-group">
            <select id="email">
                <c:forEach items="${emails}" var="item">

                    <option value="${item.id}" <c:if test="${item.id.equals(draft.emailId)}">
                        selected = selected
                    </c:if> >
                        ${item.account}
                    </option>
                </c:forEach>

            </select>
        </div>
    </div>
    <div id="error_div" class="callout callout-danger"
         style="display: none">
        <h4>Warning!</h4>

        <p id="error_msg"></p>
    </div>
    <!-- /.box-body -->
    <div class="box-footer">
        <div class="pull-right">
            <button id="saveBtn"  type="button" class="btn btn-default">
                <i class="fa fa-pencil"></i> 保存
            </button>
            <button id="send_btn" type="submit" class="btn btn-primary">
                <i class="fa fa-envelope-o"></i> 发送
            </button>
        </div>
        <button type="reset" class="btn btn-default">
            <i class="fa fa-times"></i> 删除
        </button>
</div>
    <!-- /.box-footer -->
</div>
<script src="${pageContext.request.contextPath}/res/js/list.js"></script>
<!-- 配置文件 -->
<script type="text/javascript" src="${pageContext.request.contextPath}/res/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="${pageContext.request.contextPath}/res/ueditor.all.min.js"></script>

<!-- 文件上传插件-->
<script type="text/javascript" src="${pageContext.request.contextPath}/res/js/jquery.upload.js"></script>
<!-- 实例化编辑器 -->
<script type="text/javascript">
    //ueditor配置
    var ue = UE.getEditor('container',{toolbars: [
        ['undo', 'redo'],
        ['bold', 'italic', 'underline',   'formatmatch', 'autotypeset', 'fontfamily',
            'fontsize', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc']
    ],
        elementPathEnabled:false,
        autoHeightEnabled:false,
        initialFrameHeight:300,
        enableAutoSave:false,
        saveInterval:0

    });
</script>
<script>
    $(function (){

        //发送按钮点击事件
        $("#send_btn").click(function () {
            var draftId = $("#driftId").val();
            var to = $("#to").val();
            var cc = $("#cc").val();
            var bcc = $("#bcc").val();

            if(to == ""){
                $(".error_div").css("display", "block");
                $(".error_msg").text("请输入收件人");
                return ;
            }

            if(!checkEmailFmt(to)){
                $(".error_div").css("display", "block");
                $(".error_msg").text("收件人接收方的邮箱格式不正确");
                return;
            }
            if(cc != "" && !checkEmailFmt(cc)){
                $(".error_div").css("display", "block");
                $(".error_msg").text("抄送人的邮箱格式不正确");
                return;
            }
            if(bcc != "" && !checkEmailFmt(bcc)){
                $(".error_div").css("display", "block");
                $(".error_msg").text("密送人的邮箱格式不正确");
                return;
            }

            var subject = $("#subject").val();

            var html = ue.getContent();

            var emailId = $("#email").find("option:selected").val();
            alert(emailId);
            if (subject == "") {
                $(".error_div").css("display", "block");
                $(".error_msg").text("请输入主题");
            } else {
                $.post("/mailm/mail/compose.shtml", {
                    "to": to,
                    "copyTo": cc,
                    "bcc": bcc,
                    "subject": subject,
                    "body": html,
                    "email.id":emailId,
                    "draftId":draftId
                },function (data){
                    if(data){
                        alert("发送成功！");
                    }else{
                        alert("发送失败！");
                    }
                })
            }

        });

        function checkEmailFmt(email){
            var toEmails = email.split(";");

            var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            for(var i=0;i<toEmails.length;i++){
                if(!filter.test(toEmails[i])){
                    return false;
                }
            }
            return true;
        }
        // 保存按钮点击
        $('#saveBtn').click(function (){
            saveDraft();
        })
        saveDraftInterval = setInterval(function () {
            if(ue.getContent().length){
                saveDraft();
            }
        },1000*10);


        $("#addAttachment").click(function (){

            $(this).after('<form action="${pageContext.request.contextPath}/mail/uploadAttachment.shtml" target="hideIframe" method="post" enctype="multipart/form-data"> ' +
            ' <input type="file" name="file" /><input type="hidden" value="${draftId}" name="composeId"/> <input type="submit" id="addAttachmentBtn" value="添加" /></form>  ');
        })





                //添加一条附件上传
        <%--$("#uploadFile").change(function () {--%>
            <%--var formData = new FormData($( "#uploadForm" )[0]);--%>
            <%--$.ajax({--%>
                <%--type: "POST",--%>
                <%--url:"${pageContext.request.contextPath}/mail/uploadAttachment.shtml",--%>
                <%--data: formData,--%>
                <%--async: false,--%>
                <%--cache: false,--%>
                <%--contentType: false,--%>
                <%--processData: false,--%>
                <%--error: function(request) {--%>
                    <%--alert("上传失败");--%>
                <%--},--%>
                <%--success: function(data) {--%>
<%--//                    $("#commonLayout_appcreshi").parent().html(data);--%>
                    <%--alert("上传成功");--%>
                <%--}--%>
            <%--});--%>
        <%--})--%>


    })

</script>